package com.yun2.bms.dao.impl;

import com.yun2.bms.constant.SystemConstant;
import com.yun2.bms.dao.IUserDao;
import com.yun2.bms.model.User;
import com.yun2.bms.utils.DBUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * author: youli
 * qq: 27855162@qq.com
 * date: 2020/5/11
 * 作用：定义针对用户表的各种CRUD操作，操作的实现基于BaseDaoImpl
 */

public class UserDaoImpl extends BaseDaoImpl implements IUserDao {

    public int add(User user) {
        String sql = "insert into tuser(login_name,user_name,password," +
                "gender,role,phone,email," +
                "max_lend_number,max_lend_days,create_date)" +
                "values(?,?,?,?,?,?,?,?,?,?)";
        Object[] params = {user.getLoginName(), user.getUserName(), user.getPassword(),
                user.getGender(), user.getRole(), user.getPhone(), user.getEmail(),
                user.getMaxLendNumber(), user.getMaxLendDays(), user.getCreateDate()};
        return super.insertBySql(sql, params);
    }

    public int delete(int id) {
        String sql = "delete from tuser where id=?";
        Object[] params = {id};
        return super.updateBySql(sql, params);
    }

    @Override
    public int delete(String ids) {
        String sql = "delete from tuser where id in (?)";
        Object[] params = {ids};
        return super.updateBySql(sql, params);
    }

    public int edit(User user) {
        String sql = "update tuser set login_name=?,user_name=?,gender=?," +
                "email=?,phone=?,role=? where id=?";
        Object[] params = {user.getLoginName(), user.getUserName(), user.getGender(), user.getEmail(),
                user.getPhone(), user.getRole(), user.getId()};
        return super.updateBySql(sql, params);
    }

    public User get(int id) {
        User user = null;
        try {
            String sql = "select * from tuser where id=?";
            Object[] params = {id};
            //将查询到的数据放在rs中
            super.queryBySql(sql, params);
            while (rs.next()) {
                user = new User();
                makeModel(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtils.close(conn, st, rs);
        }
        return user;
    }

    private void makeModel(User user) throws SQLException {
        user.setId(rs.getInt("id"));
        user.setLoginName(rs.getString("login_name"));
        user.setUserName(rs.getString("user_name"));
        user.setGender(rs.getString("gender"));
        user.setEmail(rs.getString("email"));
        user.setPhone(rs.getString("phone"));
        user.setRole(rs.getString("role"));
        user.setImgUrl(rs.getString("img_url"));
        user.setMaxLendNumber(rs.getInt("max_lend_number"));
        user.setMaxLendDays(rs.getInt("max_lend_days"));
        user.setCreateDate(rs.getDate("create_date"));
    }

    public List<User> getAll() {
        List<User> list = new ArrayList<User>();
        try {
            String sql = "select * from tuser";
            Object[] params = {};
            super.queryBySql(sql, params);
            while (rs.next()) {
                User user = new User();
                makeModel(user);
                list.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtils.close(conn, st, rs);
        }
        return list;
    }

    public User login(String loginName, String password) {
        User user = null;
        try {
            String sql = "select * from tuser where login_name = ? and password = ?";
            Object[] params = {loginName, password};
            //将查询到的数据放在rs中
            super.queryBySql(sql, params);
            while (rs.next()) {
                user = new User();
                makeModel(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtils.close(conn, st, rs);
        }
        return user;
    }

    @Override
    public boolean isLoginNameUnique(Integer id, String loginName) {
        int count = 0;
        String sql = "select count(*) from tuser where login_name = ? and id <> ?";
        Object[] params = {loginName, id};
        try {
            super.queryBySql(sql, params);
            while (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtils.close(conn, st, rs);
        }
        return count > 0;
    }

    @Override
    public boolean isLoginNameUnique(String loginName) {
        int count = 0;
        String sql = "select count(*) from tuser where login_name = ?";
        Object[] params = {loginName};
        try {
            super.queryBySql(sql, params);
            while (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtils.close(conn, st, rs);
        }
        return count > 0;
    }

    @Override
    public List<User> getAllByUserInfo(String userInfo) {
        String info = "%" + userInfo + "%";
        List<User> list = new ArrayList<User>();
        try {
            String sql = "select * from tuser where login_name like ? or user_name like ? or phone like ?";
            Object[] params = {info, info, info};
            super.queryBySql(sql, params);
            while (rs.next()) {
                User user = new User();
                makeModel(user);
                list.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtils.close(conn, st, rs);
        }
        return list;
    }
}
